<template>
  <div class="manager-login">
    <div class="login-content">
      <div class="login-logo">
        <el-icon size="60"><User /></el-icon>
        <h1>客户经理登录</h1>
        <p>欢迎使用客户拜访管理系统</p>
      </div>

      <el-form 
        ref="loginFormRef"
        :model="loginForm" 
        :rules="loginRules"
        class="login-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名或邮箱"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            size="large"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { ArrowLeft, User, Lock } from '@element-plus/icons-vue'
import { trpc } from '@/utils/trpc'
import { AuthUtils } from '@/utils/auth'

const loginFormRef = ref<FormInstance>()
const loading = ref(false)

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名或邮箱', trigger: 'blur' },
    { min: 3, message: '用户名至少3个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6个字符', trigger: 'blur' }
  ]
}

// 处理登录
async function handleLogin() {
  if (!loginFormRef.value) return

  try {
    const valid = await loginFormRef.value.validate()
    if (!valid) return

    loading.value = true

    // 调用后端真实登录
    const result = await trpc.auth.managerLogin.mutate({
      username: loginForm.username,
      password: loginForm.password
    })

    if (result?.success && result.data?.token && result.data?.manager) {
      AuthUtils.saveAuth(result.data.token, result.data.manager)
      ElMessage.success('登录成功！')
      window.location.href = '/'
    } else {
      throw new Error(result?.message || '登录失败')
    }
  } catch (error: any) {
    ElMessage.error(error?.message || '登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.manager-login {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.login-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  max-width: 400px;
  margin: 0 auto;
  width: 100%;
}

.login-logo {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.login-logo .el-icon {
  margin-bottom: 16px;
}

.login-logo h1 {
  font-size: 1.8rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-logo p {
  opacity: 0.9;
  font-size: 1rem;
}

.login-form {
  background: white;
  padding: 30px 20px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
}

.login-footer {
  margin-top: 20px;
  text-align: center;
}

.divider-text {
  color: white;
  opacity: 0.8;
  font-size: 14px;
}

.register-link {
  color: white !important;
  font-size: 16px;
  font-weight: 600;
}

:deep(.el-divider__text) {
  background: transparent;
}

:deep(.el-divider--horizontal) {
  border-color: rgba(255, 255, 255, 0.3);
}
</style>
